# Imports
import direction
import map
import os.path
import pygame
import sys
import xml.sax
from actor import Actor
from inputhandler import InputHandler
from map import Map
from player import Player

# Declaration of constants
SCREEN_WIDTH = 700
SCREEN_HEIGHT = 320
DESIRED_FPS = 40
RUNNING = 1

pygame.init()
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT), 0, 32)
clock = pygame.time.Clock()

base_path = sys.path[0]
dudeImage = pygame.image.load(os.path.join(base_path, 'Content', 'Images', 'gem_blue.png'))
dude = Player(dudeImage, (0,0), 100, 8)

level1 = Map(os.path.join(base_path, 'Content', 'level1.xml'))
bad_rects = level1.GetImpassableRectangles()

inputhandler = InputHandler()
inputhandler.playerKeySubscribers.append(dude.OnKeyChanged)

while RUNNING:
        # pygame.event.get() doesn't appear to always get Key.Down events once several keys have 
	# been pressed beforehand
	inputhandler.Update(pygame.event.get())

	# Try to update at 40 frames per second
	elapsedTime = clock.tick(DESIRED_FPS)

	screen.fill(0)
	level1.Draw(screen)
	dude.Draw(screen)
	dude.Update(elapsedTime, bad_rects)
	
	pygame.display.flip()
